//
//  YWSearchViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWSearchViewController.h"
#import "TYTabButtonPagerController.h"
#import "YWSearchAllViewController.h"
#import "YWSearchAlbumViewController.h"
#import "YWSearchVoiceViewController.h"
#import "YWSearchPeopleViewController.h"
#import "YWSearchVideoViewController.h"
#import "YWSearchPicModel.h"
#import "YWSearchPicTableViewCell.h"

#import "LTArchiverTools.h"
#import "YWHistoryCollectionViewCell.h"
#import "YWHistoryHeaderCollectionReusableView.h"
#import "YWSingleWordTableViewCell.h"

#import "AlbumDetailViewController.h"


@interface YWSearchViewController ()<UISearchBarDelegate, TYPagerControllerDataSource, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UISearchBar *searchBar;

@property (nonatomic, strong) TYTabButtonPagerController *pageController;

@property (nonatomic, strong) NSArray *pageDataArray;

@property (nonatomic, strong) UIView *historyView;

@property (nonatomic, strong) UITableView *searchTableView;

@property (nonatomic, strong) NSMutableArray *albumArray;

@property (nonatomic, strong) NSMutableArray *queryArray;

@property (nonatomic, strong) NSString *keyString;

@property (nonatomic, strong) UIButton *returnButton;

@property (nonatomic, strong) UIImageView *searchPlaceHolderImageView;

@property (nonatomic, strong) UIImageView *pagePlaceHolderImageView;

@property (nonatomic, strong) NSMutableArray *stringArray;

@property (nonatomic, strong) NSMutableArray *recommendArray;

@property (nonatomic, strong) UICollectionView *historyCollectionView;

@property (nonatomic, strong) UICollectionView *recommendCollectionView;

@end

@implementation YWSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    [self getView];
    [self getData];
}


- (void)getView
{
    [self getTopView];
    [self addPagerController];
    [self getSearchView];
    [self getPlaceHolderImage];
}

#pragma mark --- 顶部视图 ---
- (void)getTopView
{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 64)];
    topView.backgroundColor = [UIColor colorWithWhite:0.904 alpha:1.000];
    [self.view addSubview:topView];
    
    self.returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _returnButton.frame = CGRectMake(SCREENWIDTH - 60, 30, 40, 20);
    _returnButton.backgroundColor = [UIColor whiteColor];
    _returnButton.tag = 1000;
    [_returnButton setTitle:@"取消" forState:UIControlStateNormal];
    [_returnButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _returnButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [topView addSubview:_returnButton];
    [_returnButton addTarget:self action:@selector(didClickedReturnButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 25, SCREENWIDTH - 60, 30)];
    _searchBar.placeholder = @"搜索";
    _searchBar.backgroundImage = [[UIImage alloc] init];
    _searchBar.delegate = self;
    [topView addSubview:_searchBar];
    
    UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
    if (searchField) {
        [searchField setBackgroundColor:[UIColor whiteColor]];
        searchField.layer.cornerRadius = 13.0f;
        searchField.layer.borderColor = [UIColor grayColor].CGColor;
        searchField.layer.borderWidth = 1;
        searchField.layer.masksToBounds = YES;
    }
}

- (void)didClickedReturnButton:(UIButton *)sender
{
    if (_returnButton.tag == 1000) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        _searchTableView.hidden = YES;
        _keyString = _searchBar.text;
        [_pageController reloadData];
        
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSString *fileName = [path stringByAppendingPathComponent:@"StringArray.plist" ];
        NSLog(@"%@", fileName);
        self.stringArray = [NSMutableArray arrayWithContentsOfFile:fileName];
        if (!self.stringArray) {
            self.stringArray = [NSMutableArray array];
        }
        if (![_stringArray containsObject:_searchBar.text]) {
            [_stringArray addObject:_searchBar.text];
        }
        if (_stringArray.count > 0 && _stringArray.count <= 3) {
            _historyCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 70);
            _recommendCollectionView.frame = CGRectMake(0, 70, SCREENWIDTH, 130);
        }
        if (_stringArray.count > 3 && _stringArray.count <= 6) {
            _historyCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 100);
            _recommendCollectionView.frame = CGRectMake(0, 100, SCREENWIDTH, 130);
        }
        if (_stringArray.count > 6) {
            [_stringArray removeObjectAtIndex:0];
        }
        [_stringArray writeToFile:fileName atomically:YES];
        [_historyCollectionView reloadData];

    }
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _pageController.view.frame = CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64);
}

#pragma mark --- 分页控制器 ---
- (void)addPagerController
{
    self.pageController = [[TYTabButtonPagerController alloc]init];
    _pageController = [[TYTabButtonPagerController alloc]init];
    _pageController.dataSource = self;
    _pageController.adjustStatusBarHeight = YES;
    _pageController.barStyle = TYPagerBarStyleProgressView;
    _pageController.cellSpacing = 41;
    _pageController.progressWidth = 0;
    _pageController.view.frame = CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64);
    [self addChildViewController:_pageController];
    [self.view addSubview:_pageController.view];
}

- (NSInteger)numberOfControllersInPagerController
{
    return _pageDataArray.count;
}

- (NSString *)pagerController:(TYPagerController *)pagerController titleForIndex:(NSInteger)index
{
    return [_pageDataArray objectAtIndex:index];
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
{
    switch (index) {
        case 0:
        {
            YWSearchAllViewController *avc = [[YWSearchAllViewController alloc] init];
            avc.imageBlock = ^(void)
            {
                _pagePlaceHolderImageView.hidden = NO;
            };
            avc.keyString = _keyString;
            avc.valueBlock = ^(NSInteger num)
            {
                [_pageController moveToControllerAtIndex:num animated:YES];
            };
            return avc;
        }
            break;
        case 1:
        {
            YWSearchAlbumViewController *alvc = [[YWSearchAlbumViewController alloc] init];
            alvc.keyString = _keyString;
            return alvc;
        }
            break;
        case 2:
        {
            YWSearchVoiceViewController *vvc = [[YWSearchVoiceViewController alloc] init];
            vvc.keyString = _keyString;
            return vvc;
        }
            break;
        case 3:
        {
            YWSearchPeopleViewController *pvc = [[YWSearchPeopleViewController alloc] init];
            pvc.keyString = _keyString;
            return pvc;
        }
            break;
        default:
        {
            YWSearchVideoViewController *vvc = [[YWSearchVideoViewController alloc] init];
            vvc.keyString = _keyString;
            return vvc;
        }
            break;
    }
}

#pragma mark --- 历史界面 ---
- (void)getHistoryAndHotView
{
    self.historyView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64)];
    _historyView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_historyView];
    [self getHistoryCollectionView];
}

- (void)getHistoryCollectionView
{
    UICollectionViewFlowLayout *historyCollectionFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    historyCollectionFlowLayout.itemSize = CGSizeMake((SCREENWIDTH - 40) / 3, 20);
    historyCollectionFlowLayout.minimumLineSpacing = 10;
    historyCollectionFlowLayout.minimumInteritemSpacing =10;
    historyCollectionFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    historyCollectionFlowLayout.headerReferenceSize = CGSizeMake(SCREENWIDTH, 30);
    
    NSInteger height = 0;
    if (_stringArray.count > 0 && _stringArray.count <= 3) {
        height = 70;
    }
    if (_stringArray.count > 3 && _stringArray.count <= 6) {
        height = 100;
    }
    
    self.historyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, height) collectionViewLayout:historyCollectionFlowLayout];
    _historyCollectionView.delegate = self;
    _historyCollectionView.dataSource = self;
    _historyCollectionView.backgroundColor = [UIColor whiteColor];
    [_historyView addSubview:_historyCollectionView];
    
    [_historyCollectionView registerClass:[YWHistoryCollectionViewCell class] forCellWithReuseIdentifier:@"historyCell"];
    //注册头视图
    [_historyCollectionView registerClass:[YWHistoryHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    

}

- (void)getRecommendCollectionView
{
    UICollectionViewFlowLayout *recommendCollectionFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    recommendCollectionFlowLayout.itemSize = CGSizeMake((SCREENWIDTH - 40) / 3, 20);
    recommendCollectionFlowLayout.minimumLineSpacing = 10;
    recommendCollectionFlowLayout.minimumInteritemSpacing =10;
    recommendCollectionFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    recommendCollectionFlowLayout.headerReferenceSize = CGSizeMake(SCREENWIDTH, 30);
    
    NSInteger height = 0;
    if (_stringArray.count > 0 && _stringArray.count <= 3) {
        height = 70;
    }
    if (_stringArray.count > 3 && _stringArray.count <= 6) {
        height = 100;
    }
    
    self.recommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, height, SCREENWIDTH, 130) collectionViewLayout:recommendCollectionFlowLayout];
    _recommendCollectionView.delegate = self;
    _recommendCollectionView.dataSource = self;
    _recommendCollectionView.backgroundColor = [UIColor whiteColor];
    [_historyView addSubview:_recommendCollectionView];
    
    [_recommendCollectionView registerClass:[YWHistoryCollectionViewCell class] forCellWithReuseIdentifier:@"historyCell"];
    //注册头视图
    [_historyCollectionView registerClass:[YWHistoryHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [_recommendCollectionView registerClass:[YWHistoryHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reheaderView"];
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 1)];
    lineLabel.backgroundColor = [UIColor colorWithWhite:0.780 alpha:1.000];
    [_recommendCollectionView addSubview:lineLabel];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _historyCollectionView) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            YWHistoryHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
            headerView.cleanHistoryBlock = ^()
            {
                NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
                NSString *fileName = [path stringByAppendingPathComponent:@"StringArray.plist" ];
                NSLog(@"%@", fileName);
                self.stringArray = [NSMutableArray arrayWithContentsOfFile:fileName];
                if (!self.stringArray) {
                    self.stringArray = [NSMutableArray array];
                }
                [_stringArray removeAllObjects];
                [_stringArray writeToFile:fileName atomically:YES];
                _historyCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 0);
                _recommendCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 130);
                [_historyCollectionView reloadData];
            };
            return headerView;
        }
        else
        {
            return nil;
        }
    }
    else
    {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            YWHistoryHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reheaderView" forIndexPath:indexPath];
            headerView.historyLabel.text = @"热门搜索";
            headerView.cleanHistoryLabel.hidden = YES;
            return headerView;
        }
        else
        {
            return nil;
        }
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _historyCollectionView) {
        return _stringArray.count;
    }
    return _recommendArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _historyCollectionView) {
        YWHistoryCollectionViewCell *historyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"historyCell" forIndexPath:indexPath];
        NSString *string = [NSString stringWithFormat:@"  %@  ", [_stringArray objectAtIndex:indexPath.item]];
        historyCell.textString = string;
        return historyCell;
    }
    YWHistoryCollectionViewCell *historyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"historyCell" forIndexPath:indexPath];
    NSString *string = [NSString stringWithFormat:@"  %@  ", [_recommendArray objectAtIndex:indexPath.item]];
    historyCell.textString = string;
    return historyCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _historyCollectionView) {
        NSString *string = [_stringArray objectAtIndex:indexPath.item];
        _searchBar.text = string;
        _historyView .hidden = YES;
        _returnButton.tag = 2000;
        [_returnButton setTitle:@"搜索" forState:UIControlStateNormal];
        [self didSelectedItem];
    }
    NSString *string = [_recommendArray objectAtIndex:indexPath.item];
    _searchBar.text = string;
    _historyView .hidden = YES;
    _returnButton.tag = 2000;
    [_returnButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self didSelectedItem];
}

#pragma mark --- 搜索界面 ---
- (void)getSearchView
{
    self.searchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64) style:UITableViewStylePlain];
    _searchTableView.delegate = self;
    _searchTableView.dataSource = self;
    _searchTableView.backgroundColor = [UIColor whiteColor];
    _searchTableView.separatorStyle = 0;
    [self.view addSubview:_searchTableView];
    
    [_searchTableView registerClass:[YWSearchPicTableViewCell class] forCellReuseIdentifier:@"picCell"];
    [_searchTableView registerClass:[YWSingleWordTableViewCell class] forCellReuseIdentifier:@"textCell"];
}

- (void)getPlaceHolderImage
{
    self.searchPlaceHolderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64)];
    _searchPlaceHolderImageView.image = [UIImage imageNamed:@"搜索占位图"];
    _searchPlaceHolderImageView.hidden = YES;
    [self.searchTableView addSubview:_searchPlaceHolderImageView];
    
    self.pagePlaceHolderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64)];
    _pagePlaceHolderImageView.image = [UIImage imageNamed:@"搜索占位图"];
    _pagePlaceHolderImageView.hidden = YES;
    [self.pageController.view addSubview:_pagePlaceHolderImageView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _albumArray.count;
    }
    return _queryArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (_albumArray.count != 0) {
            YWSearchPicModel *model = [_albumArray objectAtIndex:indexPath.row];
            YWSearchPicTableViewCell *picCell = [tableView dequeueReusableCellWithIdentifier:@"picCell"];
            picCell.picModel = model;
            picCell.searchBarString = _searchBar.text;
            return picCell;
        }
    }

    YWSearchPicModel *model = [_queryArray objectAtIndex:indexPath.row];
    YWSingleWordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
    cell.picModel = model;
    cell.searchBarString = _searchBar.text;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (_albumArray.count != 0) {
            YWSearchPicModel *model = [_albumArray objectAtIndex:indexPath.row];
            _searchBar.text = model.keyword;
            _searchTableView.hidden = YES;
            _historyView.hidden = YES;
            [_searchBar resignFirstResponder];
            _keyString = model.keyword;
            [_pageController reloadData];
            AlbumDetailViewController *album = [[AlbumDetailViewController alloc] init];
            album.albmId = model.id;
            [self.navigationController pushViewController:album animated:YES];
            
        }
    }
    else
    {
        YWSearchPicModel *model = [_queryArray objectAtIndex:indexPath.row];
        _searchBar.text = model.keyword;
        _searchTableView.hidden = YES;
        _historyView.hidden = YES;
        [_searchBar resignFirstResponder];
        _keyString = model.keyword;
        [_pageController reloadData];
    }
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"StringArray.plist" ];
    NSLog(@"%@", fileName);
    self.stringArray = [NSMutableArray arrayWithContentsOfFile:fileName];
    if (!self.stringArray) {
        self.stringArray = [NSMutableArray array];
    }
    if (![_stringArray containsObject:_searchBar.text]) {
        [_stringArray addObject:_searchBar.text];
    }
    NSLog(@"%ld", _stringArray.count);
    if (_stringArray.count > 0 && _stringArray.count <= 3) {
        _historyCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 70);
        _recommendCollectionView.frame = CGRectMake(0, 70, SCREENWIDTH, 130);
    }
    if (_stringArray.count > 3 && _stringArray.count <= 6) {
        _historyCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 100);
        _recommendCollectionView.frame = CGRectMake(0, 100, SCREENWIDTH, 130);
    }
    if (_stringArray.count > 6) {
        [_stringArray removeObjectAtIndex:0];
    }
    [_stringArray writeToFile:fileName atomically:YES];
    [_historyCollectionView reloadData];
}

#pragma mark --- 请求数据 ---
- (void)getData
{
    self.albumArray = [NSMutableArray array];
    self.queryArray = [NSMutableArray array];
    self.recommendArray = [NSMutableArray array];
    [self getPageData];
    [self getRecommendData];
}

- (void)getPageData
{
    self.pageDataArray = [NSArray arrayWithObjects:@"全部", @"专辑", @"声音", @"主播", @"广播", nil];
    [_pageController reloadData];
}

- (void)getRecommendData
{
    NSString *urlString = @"http://mobile.ximalaya.com/m/hot_search_keys?device=iPhone";
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSArray *array = [result objectForKey:@"keys"];
        if (array.count > 9) {
            for (NSInteger i = 0; i < 9; i++) {
                [_recommendArray addObject:array[i]];
            }
        }
        else
        {
            for (NSString *str in array) {
                [_recommendArray addObject:str];
            }
        }
        [_recommendCollectionView reloadData];
    } Failure:^(NSError *error) {
        
    }];
}

- (void)didSelectedItem
{
    _searchTableView.hidden = YES;
    _keyString = _searchBar.text;
    [_pageController reloadData];
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"StringArray.plist" ];
    NSLog(@"%@", fileName);
    self.stringArray = [NSMutableArray arrayWithContentsOfFile:fileName];
    if (!self.stringArray) {
        self.stringArray = [NSMutableArray array];
    }
    if (![_stringArray containsObject:_searchBar.text]) {
        [_stringArray addObject:_searchBar.text];
    }
    if (_stringArray.count > 0 && _stringArray.count <= 3) {
        _historyCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 70);
        _recommendCollectionView.frame = CGRectMake(0, 70, SCREENWIDTH, 130);
    }
    if (_stringArray.count > 3 && _stringArray.count <= 6) {
        _historyCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 100);
        _recommendCollectionView.frame = CGRectMake(0, 100, SCREENWIDTH, 130);
    }
    if (_stringArray.count > 6) {
        [_stringArray removeObjectAtIndex:0];
    }
    [_stringArray writeToFile:fileName atomically:YES];
    [_historyCollectionView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    _searchPlaceHolderImageView.hidden = YES;
    _pagePlaceHolderImageView.hidden = YES;
    if ([searchText isEqualToString:@""]) {
        _historyView.hidden = NO;
        [_returnButton setTitle:@"返回" forState:UIControlStateNormal];
        _returnButton.tag = 1000;
    }
    else
    {
        _returnButton.tag = 2000;
        [_returnButton setTitle:@"搜索" forState:UIControlStateNormal];
        _searchTableView.hidden = NO;
        _historyView.hidden = YES;
        [_albumArray removeAllObjects];
        [_queryArray removeAllObjects];
        [_searchTableView reloadData];
        
        if (_albumArray.count == 0 && _queryArray.count == 0) {
            NSString *string = [NSString stringWithFormat:@"http://search.ximalaya.com/suggest?device=iPhone&kw=%@&paidFilter=false", searchText];
            [LTNetTool GetNetWithURL:string Body:nil Header:nil Response:LTJSON Success:^(id result) {
                if (_albumArray.count == 0) {
                    NSArray *albumArray = [result objectForKey:@"albumResultList"];
                    for (NSDictionary *dic in albumArray) {
                        YWSearchPicModel *picModel = [[YWSearchPicModel alloc] initWithDic:dic];
                        [_albumArray addObject:picModel];
                    }
                }
                if (_queryArray.count == 0) {
                    NSArray *queryArray = [result objectForKey:@"queryResultList"];
                    for (NSDictionary *dic in queryArray) {
                        YWSearchPicModel *model = [[YWSearchPicModel alloc] initWithDic:dic];
                        [_queryArray addObject:model];
                    }
                }
                if (_albumArray.count == 0 && _queryArray.count == 0) {
                    _searchPlaceHolderImageView.hidden = NO;
                }
                [_searchTableView reloadData];
            } Failure:^(NSError *error) {
                
            }];
        }
    }
}

#pragma mark --- 导航栏 ---
- (void)viewWillAppear:(BOOL)animated
{
    [_searchBar becomeFirstResponder];
    self.navigationController.navigationBar.hidden = YES;
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"StringArray.plist" ];
    NSLog(@"%@", fileName);
    self.stringArray = [NSMutableArray arrayWithContentsOfFile:fileName];
    
    [self getHistoryAndHotView];
    [self getRecommendCollectionView];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_searchBar endEditing:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_searchBar endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
